<?php

abstract class AbstractController
{

  /**
   *
   * @return Response
   */
  public function response($response = null)
  {
    if (! empty($response)) {
      $this->response = $response;
    }

    return $this->response;
  }

  protected function initialize()
  {
    //
  }

  protected function callAction($action)
  {
    $args = func_get_args();

    unset($args[0]);

    $func = Inflector::variablize($action . 'Action');

    if (! method_exists($this, $func)) {
      throw new ActionNotFoundException('Controller action not found');
    }

    $this->action = $action;

    $this->beforeAction($action);

    $output = call_user_func_array(array($this, $func), $args);

    $this->afterAction($action, $output);

    return $output;
  }

  protected function callActionArray($action, $params = array())
  {
    $params = (array) $params;

    array_unshift($params, $action);

    return call_user_func_array(array($this, 'callAction'), $params);
  }

}
